package org.urchr.beorganized.api.impl.user;

import org.postgresql.util.Base64;
import org.urchr.beorganized.api.base.PrivateBaseAPIServlet;
import org.urchr.beorganized.dao.UsersDAO;
import org.urchr.beorganized.dao.XMLDocumentBuilder;
import org.urchr.beorganized.dao.errors.GeneralException;
import org.urchr.beorganized.dao.tables.User;

public class UpdatePassword extends PrivateBaseAPIServlet {

	@Override
	protected void doProcessPrivate() throws GeneralException {
		String oldPass = request.getParameter("oldpassword"); 
		String newPass = request.getParameter("newpassword");
		UsersDAO.updatePassword(userID, oldPass, newPass, true);
		XMLDocumentBuilder doc = new XMLDocumentBuilder();
		User user = UsersDAO.getUser(userID);
		UsersDAO.userToNode(doc, doc.getRootElement("user"), user);
		String string = user.getLogin()+"@"+user.getPassword();
		doc.getRootElement().setAttribute("token", Base64.encodeBytes(string.getBytes()));
		sendNode(doc.getRootElement());
	}

}
